
let next = 0;

/**
 * A string template processor that works like a normal string template but appends an auto-incrementing suffix to the
 * end of every word that guarantees uniqueness when used to register D3 event handlers. The suffix has the form ".$x",
 * where x is an auto-incrementing number.
 *
 * <pre>
 *     $`a`          // "a.$0"
 *     $`a`          // "a.$1"
 *     $`a b`        // "a.$2 b.$3"
 *     $`a${"n"} b`  // "an.$4 b.$5"
 * </pre>
 *
 * @param {string[]} parts
 * @param [substitutions]
 * @returns {string} the replaced string
 */
export function $(parts, ...substitutions) {
    let result = parts[0];
    for (let i = 0; i < substitutions.length; i++) {
        result += substitutions[i] + parts[i + 1];
    }
    return result.replace(/(\S+)/g, (match, p1) => `${p1}.$${next++}`);
}

/** only for testing */
export function reset() {
    next = 0;
}
